<template>
  <div class="register">
    <h2>用户注册</h2>
    <form @submit.prevent="handleRegister" class="register-form">
      <!-- 角色选择 -->
      <div class="form-group">
        <label>选择角色</label>
        <div class="role-selector">
          <div 
            class="role-option" 
            :class="{ active: formData.role === 'parent' }"
            @click="formData.role = 'parent'"
          >
            <i class="icon">👨‍👩‍👧‍👦</i>
            <span>我是家长</span>
          </div>
          <div 
            class="role-option" 
            :class="{ active: formData.role === 'child' }"
            @click="formData.role = 'child'"
          >
            <i class="icon">👶</i>
            <span>我是孩子</span>
          </div>
        </div>
      </div>

      <!-- 基本信息 -->
      <div class="form-group">
        <label>用户名</label>
        <input type="text" v-model="formData.username" required>
      </div>
      <div class="form-group">
        <label>密码</label>
        <input type="password" v-model="formData.password" required>
      </div>
      <div class="form-group">
        <label>确认密码</label>
        <input type="password" v-model="formData.confirmPassword" required>
      </div>

      <!-- 孩子特有信息 -->
      <template v-if="formData.role === 'child'">
        <div class="form-group">
          <label>年龄</label>
          <input type="number" v-model="formData.age" min="1" max="18" required>
        </div>
        <div class="form-group">
          <label>年级</label>
          <select v-model="formData.grade" required>
            <option value="">请选择年级</option>
            <option value="幼儿园">幼儿园</option>
            <option value="一年级">一年级</option>
            <option value="二年级">二年级</option>
            <option value="三年级">三年级</option>
            <option value="四年级">四年级</option>
            <option value="五年级">五年级</option>
            <option value="六年级">六年级</option>
            <option value="初一">初一</option>
            <option value="初二">初二</option>
            <option value="初三">初三</option>
          </select>
        </div>
      </template>

      <button type="submit">注册</button>
      <p class="login-link">
        已有账号？<router-link to="/login">立即登录</router-link>
      </p>
    </form>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'Register',
  data() {
    return {
      formData: {
        role: 'parent',  // 默认选择家长角色
        username: '',
        password: '',
        confirmPassword: '',
        age: '',
        grade: ''
      }
    }
  },
  methods: {
    async handleRegister() {
      if (this.formData.password !== this.formData.confirmPassword) {
        alert('两次输入的密码不一致')
        return
      }

      try {
        if (this.formData.role === 'parent') {
          // 家长注册
          await axios.post('/api/parents', {
            username: this.formData.username,
            password: this.formData.password
          })
          
          // 注册成功后直接登录
          const loginResponse = await axios.post('/api/auth/login', {
            username: this.formData.username,
            password: this.formData.password
          })
          
          localStorage.setItem('token', loginResponse.data.token)
          localStorage.setItem('userId', loginResponse.data.user_id)
          this.$router.push('/children')
        } else {
          // 孩子注册
          await axios.post('/api/children/register', {
            username: this.formData.username,
            password: this.formData.password,
            age: this.formData.age,
            grade: this.formData.grade
          })
          
          alert('注册成功')
          this.$router.push('/login')
        }
      } catch (error) {
        console.error('注册失败:', error)
        alert(error.response?.data?.error || '注册失败')
      }
    }
  }
}
</script>

<style scoped>
.register {
  max-width: 400px;
  margin: 40px auto;
  padding: 20px;
}

.register-form {
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.form-group {
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
}

.form-group input {
  width: 100%;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.login-link {
  margin-top: 15px;
  text-align: center;
}

button {
  width: 100%;
  padding: 10px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
}

button:hover {
  background-color: #3aa876;
}

.role-selector {
  display: flex;
  gap: 20px;
  margin-top: 10px;
}

.role-option {
  flex: 1;
  padding: 15px;
  border: 2px solid #ddd;
  border-radius: 8px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.role-option.active {
  border-color: #42b983;
  background-color: #f0f9f4;
}

.role-option .icon {
  font-size: 24px;
  display: block;
  margin-bottom: 8px;
}

.role-option span {
  color: #666;
}

.role-option.active span {
  color: #42b983;
  font-weight: bold;
}

select {
  width: 100%;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
  background-color: white;
}
</style> 